{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extended Thinking with Tool Use\n",
    "\n",
    "## Table of contents\n",
    "- [Setup](#setup)\n",
    "- [Basic example](#basic-example)\n",
    "- [Multiple tool calls](#multiple-tool-calls-with-thinking)\n",
    "- [Preserving thinking blocks](#preserving-thinking-blocks)\n",
    "\n",
    "This notebook demonstrates how to use Claude 3.7 Sonnet's extended thinking feature with tools. The extended thinking feature allows you to see Claude's step-by-step thinking before it provides a final answer, providing transparency into how it decides which tools to use and how it interprets tool results.\n",
    "\n",
    "When using extended thinking with tool use, the model will show its thinking before making tool requests, but not repeat the thinking process after receiving tool results. Claude will not output another thinking block until after the next non-`tool_result` `user` turn. For more information on extended thinking, see our [documentation](https://docs.claude.com/en/docs/build-with-claude/extended-thinking)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "python"
    }
   },
   "source": [
    "## Setup\n",
    "\n",
    "First, let's install the necessary packages and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "python"
    }
   },
   "outputs": [],
   "source": [
    "%pip install anthropic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "python"
    }
   },
   "outputs": [],
   "source": [
    "import anthropic\n",
    "import os\n",
    "import json\n",
    "\n",
    "# Global variables for model and token budgets\n",
    "MODEL_NAME = \"claude-sonnet-4-5\"\n",
    "MAX_TOKENS = 4000\n",
    "THINKING_BUDGET_TOKENS = 2000\n",
    "\n",
    "# Set your API key as an environment variable or directly\n",
    "# os.environ[\"ANTHROPIC_API_KEY\"] = \"your_api_key_here\"\n",
    "\n",
    "# Initialize the client\n",
    "client = anthropic.Anthropic()\n",
    "\n",
    "# Helper functions\n",
    "def print_thinking_response(response):\n",
    "    \"\"\"Pretty print a message response with thinking blocks.\"\"\"\n",
    "    print(\"\\n==== FULL RESPONSE ====\")\n",
    "    for block in response.content:\n",
    "        if block.type == \"thinking\":\n",
    "            print(\"\\n🧠 THINKING BLOCK:\")\n",
    "            # Show truncated thinking for readability \n",
    "            print(block.thinking[:500] + \"...\" if len(block.thinking) > 500 else block.thinking)\n",
    "            print(f\"\\n[Signature available: {bool(getattr(block, 'signature', None))}]\")\n",
    "            if hasattr(block, 'signature') and block.signature:\n",
    "                print(f\"[Signature (first 50 chars): {block.signature[:50]}...]\")\n",
    "        elif block.type == \"redacted_thinking\":\n",
    "            print(\"\\n🔒 REDACTED THINKING BLOCK:\")\n",
    "            print(f\"[Data length: {len(block.data) if hasattr(block, 'data') else 'N/A'}]\")\n",
    "        elif block.type == \"text\":\n",
    "            print(\"\\n✓ FINAL ANSWER:\")\n",
    "            print(block.text)\n",
    "    \n",
    "    print(\"\\n==== END RESPONSE ====\")\n",
    "\n",
    "def count_tokens(messages, tools=None):\n",
    "    \"\"\"Count tokens for a given message list with optional tools.\"\"\"\n",
    "    if tools:\n",
    "        response = client.messages.count_tokens(\n",
    "            model=MODEL_NAME,\n",
    "            messages=messages,\n",
    "            tools=tools\n",
    "        )\n",
    "    else:\n",
    "        response = client.messages.count_tokens(\n",
    "            model=MODEL_NAME,\n",
    "            messages=messages\n",
    "        )\n",
    "    return response.input_tokens"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Single tool calls with thinking\n",
    "\n",
    "This example demonstrates how to combine thinking and make a single tool call, with a mock weather tool."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "python"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== INITIAL RESPONSE ===\n",
      "Response ID: msg_01NhR4vE9nVh2sHs5fXbzji8\n",
      "Stop reason: tool_use\n",
      "Model: claude-sonnet-4-5\n",
      "Content blocks: 3 blocks\n",
      "\n",
      "Block 1: Type = thinking\n",
      "Thinking content: The user is asking about the current weather in Paris. I can use the `weather` function to get this information.\n",
      "\n",
      "The `weather` function requires a \"l...\n",
      "Signature available: True\n",
      "\n",
      "Block 2: Type = text\n",
      "Text content: I'll check the current weather in Paris for you.\n",
      "\n",
      "Block 3: Type = tool_use\n",
      "Tool: weather\n",
      "Tool input: {'location': 'Paris'}\n",
      "Tool ID: toolu_01WaeSyitUGJFaaPe68cJuEv\n",
      "=== END INITIAL RESPONSE ===\n",
      "\n",
      "\n",
      "=== EXECUTING TOOL ===\n",
      "Tool name: weather\n",
      "Location to check: Paris\n",
      "Result: {'temperature': 65, 'condition': 'Rainy'}\n",
      "=== TOOL EXECUTION COMPLETE ===\n",
      "\n",
      "\n",
      "=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===\n",
      "Follow-up response received. Stop reason: end_turn\n",
      "\n",
      "==== FULL RESPONSE ====\n",
      "\n",
      "✓ FINAL ANSWER:\n",
      "Currently in Paris, it's 65°F (18°C) and rainy. You might want to bring an umbrella if you're heading out!\n",
      "\n",
      "==== END RESPONSE ====\n"
     ]
    }
   ],
   "source": [
    "def tool_use_with_thinking():\n",
    "    # Define a weather tool\n",
    "    tools = [\n",
    "        {\n",
    "            \"name\": \"weather\",\n",
    "            \"description\": \"Get current weather information for a location.\",\n",
    "            \"input_schema\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"location\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The location to get weather for.\"\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"location\"]\n",
    "            }\n",
    "        }\n",
    "    ]\n",
    "    \n",
    "    def weather(location):\n",
    "        # Mock weather data\n",
    "        weather_data = {\n",
    "            \"New York\": {\"temperature\": 72, \"condition\": \"Sunny\"},\n",
    "            \"London\": {\"temperature\": 62, \"condition\": \"Cloudy\"},\n",
    "            \"Tokyo\": {\"temperature\": 80, \"condition\": \"Partly cloudy\"},\n",
    "            \"Paris\": {\"temperature\": 65, \"condition\": \"Rainy\"},\n",
    "            \"Sydney\": {\"temperature\": 85, \"condition\": \"Clear\"},\n",
    "            \"Berlin\": {\"temperature\": 60, \"condition\": \"Foggy\"},\n",
    "        }\n",
    "        \n",
    "        return weather_data.get(location, {\"error\": f\"No weather data available for {location}\"})\n",
    "    \n",
    "    # Initial request with tool use and thinking\n",
    "    response = client.messages.create(\n",
    "        model=MODEL_NAME,\n",
    "        max_tokens=MAX_TOKENS,\n",
    "        thinking={\n",
    "            \"type\": \"enabled\",\n",
    "            \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "        },\n",
    "        tools=tools,\n",
    "        messages=[{\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"What's the weather like in Paris today?\"\n",
    "        }]\n",
    "    )\n",
    "    \n",
    "    # Detailed diagnostic output of initial response\n",
    "    print(\"\\n=== INITIAL RESPONSE ===\")\n",
    "    print(f\"Response ID: {response.id}\")\n",
    "    print(f\"Stop reason: {response.stop_reason}\")\n",
    "    print(f\"Model: {response.model}\")\n",
    "    print(f\"Content blocks: {len(response.content)} blocks\")\n",
    "    \n",
    "    for i, block in enumerate(response.content):\n",
    "        print(f\"\\nBlock {i+1}: Type = {block.type}\")\n",
    "        if block.type == \"thinking\":\n",
    "            print(f\"Thinking content: {block.thinking[:150]}...\")\n",
    "            print(f\"Signature available: {bool(getattr(block, 'signature', None))}\")\n",
    "        elif block.type == \"text\":\n",
    "            print(f\"Text content: {block.text}\")\n",
    "        elif block.type == \"tool_use\":\n",
    "            print(f\"Tool: {block.name}\")\n",
    "            print(f\"Tool input: {block.input}\")\n",
    "            print(f\"Tool ID: {block.id}\")\n",
    "    print(\"=== END INITIAL RESPONSE ===\\n\")\n",
    "    \n",
    "    # Extract thinking blocks to include in the conversation history\n",
    "    assistant_blocks = []\n",
    "    for block in response.content:\n",
    "        if block.type in [\"thinking\", \"redacted_thinking\", \"tool_use\"]:\n",
    "            assistant_blocks.append(block)\n",
    "            \n",
    "    # Handle tool use if required\n",
    "    full_conversation = [{\n",
    "        \"role\": \"user\",\n",
    "        \"content\": \"What's the weather like in Paris today?\"\n",
    "    }]\n",
    "    \n",
    "    if response.stop_reason == \"tool_use\":\n",
    "        # Add entire assistant response with thinking blocks and tool use\n",
    "        full_conversation.append({\n",
    "            \"role\": \"assistant\",\n",
    "            \"content\": assistant_blocks\n",
    "        })\n",
    "        \n",
    "        # Find the tool_use block\n",
    "        tool_use_block = next((block for block in response.content if block.type == \"tool_use\"), None)\n",
    "        if tool_use_block:\n",
    "            # Execute the tool\n",
    "            print(f\"\\n=== EXECUTING TOOL ===\")\n",
    "            print(f\"Tool name: {tool_use_block.name}\")\n",
    "            print(f\"Location to check: {tool_use_block.input['location']}\")\n",
    "            tool_result = weather(tool_use_block.input[\"location\"])\n",
    "            print(f\"Result: {tool_result}\")\n",
    "            print(\"=== TOOL EXECUTION COMPLETE ===\\n\")\n",
    "            \n",
    "            # Add tool result to conversation\n",
    "            full_conversation.append({\n",
    "                \"role\": \"user\",\n",
    "                \"content\": [{\n",
    "                    \"type\": \"tool_result\",\n",
    "                    \"tool_use_id\": tool_use_block.id,\n",
    "                    \"content\": json.dumps(tool_result)\n",
    "                }]\n",
    "            })\n",
    "            \n",
    "            # Continue the conversation with the same thinking configuration\n",
    "            print(\"\\n=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===\")\n",
    "            response = client.messages.create(\n",
    "                model=MODEL_NAME,\n",
    "                max_tokens=MAX_TOKENS,\n",
    "                thinking={\n",
    "                    \"type\": \"enabled\",\n",
    "                    \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "                },\n",
    "                tools=tools,\n",
    "                messages=full_conversation\n",
    "            )\n",
    "            print(f\"Follow-up response received. Stop reason: {response.stop_reason}\")\n",
    "    \n",
    "    print_thinking_response(response)\n",
    "\n",
    "# Run the example\n",
    "tool_use_with_thinking()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Multiple tool calls with thinking\n",
    "\n",
    "This example demonstrates how to handle multiple tool calls, such as a mock news and weather service, while observing the thinking process."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "python"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== INITIAL RESPONSE ===\n",
      "Response ID: msg_01VwqpBMARVoTP1H8Ytvmvsb\n",
      "Stop reason: tool_use\n",
      "Model: claude-sonnet-4-5\n",
      "Content blocks: 3 blocks\n",
      "\n",
      "Block 1: Type = thinking\n",
      "Thinking content: The user is asking for two pieces of information:\n",
      "1. The weather in London\n",
      "2. The latest news about technology\n",
      "\n",
      "Let me check what tools I have availab...\n",
      "Signature available: True\n",
      "\n",
      "Block 2: Type = text\n",
      "Text content: I'll get that information for you right away.\n",
      "\n",
      "Block 3: Type = tool_use\n",
      "Tool: weather\n",
      "Tool input: {'location': 'London'}\n",
      "Tool ID: toolu_016xHQWMR4JsKtWvH9nbsZyA\n",
      "=== END INITIAL RESPONSE ===\n",
      "\n",
      "\n",
      "=== TOOL USE ITERATION 1 ===\n",
      "\n",
      "=== EXECUTING TOOL ===\n",
      "Tool name: weather\n",
      "Location to check: London\n",
      "Result: {'temperature': 62, 'condition': 'Cloudy'}\n",
      "=== TOOL EXECUTION COMPLETE ===\n",
      "\n",
      "\n",
      "=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===\n",
      "\n",
      "=== FOLLOW-UP RESPONSE (ITERATION 1) ===\n",
      "Response ID: msg_01EhR96Z2Z2t5EDhuWeodUod\n",
      "Stop reason: tool_use\n",
      "Content blocks: 1 blocks\n",
      "\n",
      "Block 1: Type = tool_use\n",
      "Tool: news\n",
      "Tool input preview: {'topic': 'technology'}\n",
      "=== END FOLLOW-UP RESPONSE (ITERATION 1) ===\n",
      "\n",
      "\n",
      "=== TOOL USE ITERATION 2 ===\n",
      "\n",
      "=== EXECUTING TOOL ===\n",
      "Tool name: news\n",
      "Topic to check: technology\n",
      "Result: {'headlines': ['New AI breakthrough announced by research lab', 'Tech company releases latest smartphone model', 'Quantum computing reaches milestone achievement']}\n",
      "=== TOOL EXECUTION COMPLETE ===\n",
      "\n",
      "\n",
      "=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===\n",
      "\n",
      "=== FOLLOW-UP RESPONSE (ITERATION 2) ===\n",
      "Response ID: msg_01WUEfC4UxPFaJaktjVDMJEN\n",
      "Stop reason: end_turn\n",
      "Content blocks: 1 blocks\n",
      "\n",
      "Block 1: Type = text\n",
      "Text content preview: Here's the information you requested:\n",
      "\n",
      "## Weather in London\n",
      "Currently, it's 62°F and cloudy in Londo...\n",
      "=== END FOLLOW-UP RESPONSE (ITERATION 2) ===\n",
      "\n",
      "\n",
      "=== FINAL RESPONSE ===\n",
      "\n",
      "==== FULL RESPONSE ====\n",
      "\n",
      "✓ FINAL ANSWER:\n",
      "Here's the information you requested:\n",
      "\n",
      "## Weather in London\n",
      "Currently, it's 62°F and cloudy in London.\n",
      "\n",
      "## Latest Technology News Headlines\n",
      "- New AI breakthrough announced by research lab\n",
      "- Tech company releases latest smartphone model\n",
      "- Quantum computing reaches milestone achievement\n",
      "\n",
      "==== END RESPONSE ====\n",
      "=== END FINAL RESPONSE ===\n"
     ]
    }
   ],
   "source": [
    "def multiple_tool_calls_with_thinking():\n",
    "    # Define tools\n",
    "    tools = [\n",
    "        {\n",
    "            \"name\": \"weather\",\n",
    "            \"description\": \"Get current weather information for a location.\",\n",
    "            \"input_schema\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"location\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The location to get weather for.\"\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"location\"]\n",
    "            }\n",
    "        },\n",
    "        {\n",
    "            \"name\": \"news\",\n",
    "            \"description\": \"Get latest news headlines for a topic.\",\n",
    "            \"input_schema\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"topic\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The topic to get news about.\"\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"topic\"]\n",
    "            }\n",
    "        }\n",
    "    ]\n",
    "    \n",
    "    def weather(location):\n",
    "        # Mock weather data\n",
    "        weather_data = {\n",
    "            \"New York\": {\"temperature\": 72, \"condition\": \"Sunny\"},\n",
    "            \"London\": {\"temperature\": 62, \"condition\": \"Cloudy\"},\n",
    "            \"Tokyo\": {\"temperature\": 80, \"condition\": \"Partly cloudy\"},\n",
    "            \"Paris\": {\"temperature\": 65, \"condition\": \"Rainy\"},\n",
    "            \"Sydney\": {\"temperature\": 85, \"condition\": \"Clear\"},\n",
    "            \"Berlin\": {\"temperature\": 60, \"condition\": \"Foggy\"},\n",
    "        }\n",
    "        \n",
    "        return weather_data.get(location, {\"error\": f\"No weather data available for {location}\"})\n",
    "    \n",
    "    def news(topic):\n",
    "        # Mock news data\n",
    "        news_data = {\n",
    "            \"technology\": [\n",
    "                \"New AI breakthrough announced by research lab\",\n",
    "                \"Tech company releases latest smartphone model\",\n",
    "                \"Quantum computing reaches milestone achievement\"\n",
    "            ],\n",
    "            \"sports\": [\n",
    "                \"Local team wins championship game\",\n",
    "                \"Star player signs record-breaking contract\",\n",
    "                \"Olympic committee announces host city for 2036\"\n",
    "            ],\n",
    "            \"weather\": [\n",
    "                \"Storm system developing in the Atlantic\",\n",
    "                \"Record temperatures recorded across Europe\",\n",
    "                \"Climate scientists release new research findings\"\n",
    "            ]\n",
    "        }\n",
    "        \n",
    "        return {\"headlines\": news_data.get(topic.lower(), [\"No news available for this topic\"])}\n",
    "    \n",
    "    # Initial request\n",
    "    response = client.messages.create(\n",
    "        model=MODEL_NAME,\n",
    "        max_tokens=MAX_TOKENS,\n",
    "        thinking={\n",
    "                \"type\": \"enabled\",\n",
    "                \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "        },\n",
    "        tools=tools,\n",
    "        messages=[{\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"What's the weather in London, and can you also tell me the latest news about technology?\"\n",
    "        }]\n",
    "    )\n",
    "    \n",
    "    # Print detailed information about initial response\n",
    "    print(\"\\n=== INITIAL RESPONSE ===\")\n",
    "    print(f\"Response ID: {response.id}\")\n",
    "    print(f\"Stop reason: {response.stop_reason}\")\n",
    "    print(f\"Model: {response.model}\")\n",
    "    print(f\"Content blocks: {len(response.content)} blocks\")\n",
    "    \n",
    "    # Print each content block\n",
    "    for i, block in enumerate(response.content):\n",
    "        print(f\"\\nBlock {i+1}: Type = {block.type}\")\n",
    "        if block.type == \"thinking\":\n",
    "            print(f\"Thinking content: {block.thinking[:150]}...\")\n",
    "            print(f\"Signature available: {bool(getattr(block, 'signature', None))}\")\n",
    "        elif block.type == \"text\":\n",
    "            print(f\"Text content: {block.text}\")\n",
    "        elif block.type == \"tool_use\":\n",
    "            print(f\"Tool: {block.name}\")\n",
    "            print(f\"Tool input: {block.input}\")\n",
    "            print(f\"Tool ID: {block.id}\")\n",
    "    print(\"=== END INITIAL RESPONSE ===\\n\")\n",
    "    \n",
    "    # Handle potentially multiple tool calls\n",
    "    full_conversation = [{\n",
    "        \"role\": \"user\",\n",
    "        \"content\": \"What's the weather in London, and can you also tell me the latest news about technology?\"\n",
    "    }]\n",
    "    \n",
    "    # Track iteration count for multi-turn tool use\n",
    "    iteration = 0\n",
    "    \n",
    "    while response.stop_reason == \"tool_use\":\n",
    "        iteration += 1\n",
    "        print(f\"\\n=== TOOL USE ITERATION {iteration} ===\")\n",
    "        \n",
    "        # Extract thinking blocks and tool use to include in conversation history\n",
    "        assistant_blocks = []\n",
    "        for block in response.content:\n",
    "            if block.type in [\"thinking\", \"redacted_thinking\", \"tool_use\"]:\n",
    "                assistant_blocks.append(block)\n",
    "        \n",
    "        # Add assistant response with thinking blocks and tool use\n",
    "        full_conversation.append({\n",
    "            \"role\": \"assistant\",\n",
    "            \"content\": assistant_blocks\n",
    "        })\n",
    "        \n",
    "        # Find the tool_use block\n",
    "        tool_use_block = next((block for block in response.content if block.type == \"tool_use\"), None)\n",
    "        if tool_use_block:\n",
    "            print(f\"\\n=== EXECUTING TOOL ===\")\n",
    "            print(f\"Tool name: {tool_use_block.name}\")\n",
    "            \n",
    "            # Execute the appropriate tool\n",
    "            if tool_use_block.name == \"weather\":\n",
    "                print(f\"Location to check: {tool_use_block.input['location']}\")\n",
    "                tool_result = weather(tool_use_block.input[\"location\"])\n",
    "            elif tool_use_block.name == \"news\":\n",
    "                print(f\"Topic to check: {tool_use_block.input['topic']}\")\n",
    "                tool_result = news(tool_use_block.input[\"topic\"])\n",
    "            else:\n",
    "                tool_result = {\"error\": \"Unknown tool\"}\n",
    "                \n",
    "            print(f\"Result: {tool_result}\")\n",
    "            print(\"=== TOOL EXECUTION COMPLETE ===\\n\")\n",
    "            \n",
    "            # Add tool result to conversation\n",
    "            full_conversation.append({\n",
    "                \"role\": \"user\",\n",
    "                \"content\": [{\n",
    "                    \"type\": \"tool_result\",\n",
    "                    \"tool_use_id\": tool_use_block.id,\n",
    "                    \"content\": json.dumps(tool_result)\n",
    "                }]\n",
    "            })\n",
    "            \n",
    "            # Continue the conversation\n",
    "            print(\"\\n=== SENDING FOLLOW-UP REQUEST WITH TOOL RESULT ===\")\n",
    "            response = client.messages.create(\n",
    "                model=MODEL_NAME,\n",
    "                max_tokens=MAX_TOKENS,\n",
    "                thinking={\n",
    "                        \"type\": \"enabled\",\n",
    "                        \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "                },\n",
    "                tools=tools,\n",
    "                messages=full_conversation\n",
    "            )\n",
    "            \n",
    "            # Print follow-up response details\n",
    "            print(f\"\\n=== FOLLOW-UP RESPONSE (ITERATION {iteration}) ===\")\n",
    "            print(f\"Response ID: {response.id}\")\n",
    "            print(f\"Stop reason: {response.stop_reason}\")\n",
    "            print(f\"Content blocks: {len(response.content)} blocks\")\n",
    "            \n",
    "            for i, block in enumerate(response.content):\n",
    "                print(f\"\\nBlock {i+1}: Type = {block.type}\")\n",
    "                if block.type == \"thinking\":\n",
    "                    print(f\"Thinking content preview: {block.thinking[:100]}...\")\n",
    "                elif block.type == \"text\":\n",
    "                    print(f\"Text content preview: {block.text[:100]}...\")\n",
    "                elif block.type == \"tool_use\":\n",
    "                    print(f\"Tool: {block.name}\")\n",
    "                    print(f\"Tool input preview: {str(block.input)[:100]}\")\n",
    "            print(f\"=== END FOLLOW-UP RESPONSE (ITERATION {iteration}) ===\\n\")\n",
    "            \n",
    "            if response.stop_reason != \"tool_use\":\n",
    "                print(\"\\n=== FINAL RESPONSE ===\")\n",
    "                print_thinking_response(response)\n",
    "                print(\"=== END FINAL RESPONSE ===\")\n",
    "        else:\n",
    "            print(\"No tool_use block found in response.\")\n",
    "            break\n",
    "\n",
    "# Run the example\n",
    "multiple_tool_calls_with_thinking()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "bat"
    }
   },
   "source": [
    "## Preserving thinking blocks\n",
    "\n",
    "When working with extended thinking and tools, make sure to:\n",
    "\n",
    "1. **Preserve thinking block signatures**: Each thinking block contains a cryptographic signature that validates the conversation context. These signatures must be included when passing thinking blocks back to Claude.\n",
    "\n",
    "2. **Avoid modifying prior context**: The API will reject requests if any previous content (including thinking blocks) is modified when submitting a new request with tool results.\n",
    "\n",
    "3. **Handle both thinking and redacted_thinking blocks**: Both types of blocks must be preserved in the conversation history, even if the content of redacted blocks is not human readable.\n",
    "\n",
    "For more details on extended thinking without tools, see the main \"Extended Thinking\" notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "python"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "=== INITIAL RESPONSE ===\n",
      "Response contains:\n",
      "- 1 thinking blocks\n",
      "- 1 tool use blocks\n",
      "\n",
      "Tool called: weather\n",
      "Location to check: Berlin\n",
      "Tool result: {'temperature': 60, 'condition': 'Foggy'}\n",
      "\n",
      "=== TEST 1: WITHOUT thinking block ===\n",
      "ERROR: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.1.content.0.type: Expected `thinking` or `redacted_thinking`, but found `tool_use`. When `thinking` is enabled, a final `assistant` message must start with a thinking block (preceeding the lastmost set of `tool_use` and `tool_result` blocks). We recommend you include thinking blocks from previous turns. To avoid this requirement, disable `thinking`. Please consult our documentation at https://docs.claude.com/en/docs/build-with-claude/extended-thinking'}}\n",
      "This demonstrates that thinking blocks must be preserved\n",
      "\n",
      "=== TEST 2: WITH thinking block (correct approach) ===\n",
      "SUCCESS: Response received with thinking blocks included\n",
      "\n",
      "Second response contains:\n",
      "- 0 thinking blocks\n",
      "- 1 text blocks\n",
      "\n",
      "Final answer: Currently in Berlin, it's foggy with a temperature of 60°F (about 15.5°C).\n",
      "\n",
      "Note: The second response after tool use doesn't contain thinking blocks.\n",
      "This is expected behavior - thinking is shown before tool use but not after receiving tool results.\n"
     ]
    }
   ],
   "source": [
    "def thinking_block_preservation_example():\n",
    "    # Define a simple weather tool\n",
    "    tools = [\n",
    "        {\n",
    "            \"name\": \"weather\",\n",
    "            \"description\": \"Get current weather information for a location.\",\n",
    "            \"input_schema\": {\n",
    "                \"type\": \"object\",\n",
    "                \"properties\": {\n",
    "                    \"location\": {\n",
    "                        \"type\": \"string\",\n",
    "                        \"description\": \"The location to get weather for.\"\n",
    "                    }\n",
    "                },\n",
    "                \"required\": [\"location\"]\n",
    "            }\n",
    "        }\n",
    "    ]\n",
    "    \n",
    "    def weather(location):\n",
    "        # Mock weather data\n",
    "        weather_data = {\n",
    "            \"New York\": {\"temperature\": 72, \"condition\": \"Sunny\"},\n",
    "            \"London\": {\"temperature\": 62, \"condition\": \"Cloudy\"},\n",
    "            \"Tokyo\": {\"temperature\": 80, \"condition\": \"Partly cloudy\"},\n",
    "            \"Paris\": {\"temperature\": 65, \"condition\": \"Rainy\"},\n",
    "            \"Sydney\": {\"temperature\": 85, \"condition\": \"Clear\"},\n",
    "            \"Berlin\": {\"temperature\": 60, \"condition\": \"Foggy\"},\n",
    "        }\n",
    "        \n",
    "        return weather_data.get(location, {\"error\": f\"No weather data available for {location}\"})\n",
    "    \n",
    "    # Initial request with tool use and thinking\n",
    "    response = client.messages.create(\n",
    "        model=MODEL_NAME,\n",
    "        max_tokens=MAX_TOKENS,\n",
    "        thinking={\n",
    "            \"type\": \"enabled\",\n",
    "            \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "        },\n",
    "        tools=tools,\n",
    "        messages=[{\n",
    "            \"role\": \"user\",\n",
    "            \"content\": \"What's the weather like in Berlin right now?\"\n",
    "        }]\n",
    "    )\n",
    "    \n",
    "    # Extract blocks from response\n",
    "    thinking_blocks = [b for b in response.content if b.type == \"thinking\"]\n",
    "    tool_use_blocks = [b for b in response.content if b.type == \"tool_use\"]\n",
    "    \n",
    "    print(\"\\n=== INITIAL RESPONSE ===\")\n",
    "    print(f\"Response contains:\")\n",
    "    print(f\"- {len(thinking_blocks)} thinking blocks\")\n",
    "    print(f\"- {len(tool_use_blocks)} tool use blocks\")\n",
    "    \n",
    "    # Check if tool use was triggered\n",
    "    if tool_use_blocks:\n",
    "        tool_block = tool_use_blocks[0]\n",
    "        print(f\"\\nTool called: {tool_block.name}\")\n",
    "        print(f\"Location to check: {tool_block.input['location']}\")\n",
    "        \n",
    "        # Execute the tool\n",
    "        tool_result = weather(tool_block.input[\"location\"])\n",
    "        print(f\"Tool result: {tool_result}\")\n",
    "        \n",
    "        # First, let's try WITHOUT including the thinking block\n",
    "        print(\"\\n=== TEST 1: WITHOUT thinking block ===\")\n",
    "        try:\n",
    "            # Notice we're only including the tool_use block, not the thinking block\n",
    "            partial_blocks = tool_use_blocks\n",
    "            \n",
    "            incomplete_response = client.messages.create(\n",
    "                model=MODEL_NAME,\n",
    "                max_tokens=MAX_TOKENS,\n",
    "                thinking={\n",
    "                        \"type\": \"enabled\",\n",
    "                        \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "                },\n",
    "                tools=tools,\n",
    "                messages=[\n",
    "                    {\"role\": \"user\", \"content\": \"What's the weather like in Berlin right now?\"},\n",
    "                    {\"role\": \"assistant\", \"content\": partial_blocks},\n",
    "                    {\"role\": \"user\", \"content\": [{\n",
    "                        \"type\": \"tool_result\",\n",
    "                        \"tool_use_id\": tool_block.id,\n",
    "                        \"content\": json.dumps(tool_result)\n",
    "                    }]}\n",
    "                ]\n",
    "            )\n",
    "            print(\"SUCCESS: Response received without thinking block (not expected)\")\n",
    "        except Exception as e:\n",
    "            print(f\"ERROR: {e}\")\n",
    "            print(\"This demonstrates that thinking blocks must be preserved\")\n",
    "        \n",
    "        # Now try WITH the thinking block included (correct approach)\n",
    "        print(\"\\n=== TEST 2: WITH thinking block (correct approach) ===\")\n",
    "        try:\n",
    "            # Include all blocks from the response\n",
    "            complete_blocks = thinking_blocks + tool_use_blocks\n",
    "            \n",
    "            complete_response = client.messages.create(\n",
    "                model=MODEL_NAME,\n",
    "                max_tokens=MAX_TOKENS,\n",
    "                thinking={\n",
    "                    \"type\": \"enabled\",\n",
    "                    \"budget_tokens\": THINKING_BUDGET_TOKENS\n",
    "                },\n",
    "                tools=tools,\n",
    "                messages=[\n",
    "                    {\"role\": \"user\", \"content\": \"What's the weather like in Berlin right now?\"},\n",
    "                    {\"role\": \"assistant\", \"content\": complete_blocks},\n",
    "                    {\"role\": \"user\", \"content\": [{\n",
    "                        \"type\": \"tool_result\",\n",
    "                        \"tool_use_id\": tool_block.id,\n",
    "                        \"content\": json.dumps(tool_result)\n",
    "                    }]}\n",
    "                ]\n",
    "            )\n",
    "            print(\"SUCCESS: Response received with thinking blocks included\")\n",
    "            \n",
    "            # Check if second response has thinking blocks\n",
    "            second_thinking = [b for b in complete_response.content if b.type == \"thinking\"]\n",
    "            second_text = [b for b in complete_response.content if b.type == \"text\"]\n",
    "            \n",
    "            print(f\"\\nSecond response contains:\")\n",
    "            print(f\"- {len(second_thinking)} thinking blocks\")\n",
    "            print(f\"- {len(second_text)} text blocks\")\n",
    "            \n",
    "            if second_text:\n",
    "                print(f\"\\nFinal answer: {second_text[0].text}\")\n",
    "            \n",
    "            print(\"\\nNote: The second response after tool use doesn't contain thinking blocks.\")\n",
    "            print(\"This is expected behavior - thinking is shown before tool use but not after receiving tool results.\")\n",
    "            \n",
    "        except Exception as e:\n",
    "            print(f\"ERROR: {e}\")\n",
    "    \n",
    "# Uncomment to run the example\n",
    "thinking_block_preservation_example()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Conclusion\n",
    "\n",
    "This notebook shows how to combine Claude's extended thinking feature with tool use. Key benefits include:\n",
    "\n",
    "1. Transparency into Claude's thinking process when using tools\n",
    "2. Visibility into how Claude decides when to use tools versus internal knowledge\n",
    "3. Better understanding of multi-step tasks that involve multiple tool calls\n",
    "4. Insight into how Claude interprets tool results and incorporates them into responses\n",
    "\n",
    "When using extended thinking with tools, keep in mind:\n",
    "- Set appropriate thinking budgets for complex tasks (minimum 1,024 tokens)\n",
    "- Always preserve thinking blocks and their signatures when passing tool results\n",
    "- Include both normal and redacted thinking blocks in the conversation history\n",
    "- Ensure that system prompts, tools, and thinking configurations match between calls\n",
    "- Expect that tool result turns will not contain additional thinking blocks\n",
    "- Tool use and thinking together can increase token usage and response time"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Coconut",
   "language": "coconut",
   "name": "coconut"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "python",
    "version": 3
   },
   "file_extension": ".coco",
   "mimetype": "text/x-python3",
   "name": "coconut",
   "pygments_lexer": "coconut",
   "version": "3.0.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
